package com.hy.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.ruiji.common.R;
import com.hy.ruiji.dto.DishDto;
import com.hy.ruiji.enyity.Category;
import com.hy.ruiji.enyity.Dish;
import com.hy.ruiji.enyity.DishFlavor;
import com.hy.ruiji.enyity.Employee;
import com.hy.ruiji.service.CategoryService;
import com.hy.ruiji.service.DishFlavorService;
import com.hy.ruiji.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     * @param dishDto 菜品信息
     * @return 结果返回
     */
    @PostMapping
    public R<String> saveDish(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }

    /**
     * 菜品实现分页查询
     * @param page 页数
     * @param pageSize 页码
     * @param name 菜品名字
     * @return 返回查询结果
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage=new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //添加过滤器
        wrapper.like(name !=null,Dish::getName ,name);
        wrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo,wrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list=records.stream().map((item)->{
            DishDto dishDto = new DishDto();
            //对象拷贝
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            //根据id查分类对象
            Category category = categoryService.getById(categoryId);
            //获取菜品的名称
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);

            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    /**
     * 根据id查询菜品
     * @param id 需要查询的菜品id
     * @return 返回查询结果数据
     */
    @GetMapping("/{id}")
    public R<DishDto> queryDishById(@PathVariable Long id){
        DishDto dishDto = dishService.queryDishById(id);
        return R.success(dishDto);
    }
    /**
     * 新增菜品
     * @param dishDto 菜品信息
     * @return 结果返回
     */
    @PutMapping
    public R<String> UpdateDish(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }
    @PostMapping("/status/{status}")
    public R<String> update(@PathVariable int status,Long[] ids){
        dishService.updateStatus(status,ids);
        return R.success("修改成功");
    }
    @DeleteMapping
    public R<String> removeByIds(Long[] ids){
        dishService.deleteByIds(ids);
        return R.success("删除菜品成功！");
    }

    /**
     * 根据条件查询对应的菜品
     * @param dish 查询条件
     * @return 返回菜品集合
     */
    @GetMapping("/list")
    public R<List<DishDto>> queryDishById(Dish dish){
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //根据id查
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //根据名字模糊查询
        wrapper.like(dish.getName()!=null,Dish::getName,dish.getName());
        //只查起售的菜品
        wrapper.eq(Dish::getStatus,1);
        wrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(wrapper);

        List<DishDto> list=dishList.stream().map((item)->{
            DishDto dishDto = new DishDto();
            //对象拷贝
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            //根据id查分类对象
            Category category = categoryService.getById(categoryId);
            //获取菜品的名称
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            //菜品id
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,dishId);
            //查询口味信息
            List<DishFlavor> list1 = dishFlavorService.list(wrapper1);
            //把口味信息设置到dishDto里面
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(list);
    }
}
